﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmHoThu : Form
    {
        public FrmHoThu()
        {
            InitializeComponent();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab==tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Hothut;
            }
            else
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Hothum;
            }
        }

        private void FrmHoThu_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmHoThu_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(533, 558);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrHothu prHothu = new PrHothu();
            prHothu.Show();
            this.Hide();
        }

        private void FrmHoThu_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            Values = TxtSauDg.Text;//0
            Values = Values +"\t"+ TxtDayMongN.Text;//1
            Values = Values +"\t"+ txtDaymongT.Text;//2
            Values = Values +"\t"+ TxtDayThanhHt.Text;//3
            Values = Values +"\t"+ TxtMorongmong.Text;//4
            Values = Values +"\t"+ TxtRongHt.Text;//5
            Values = Values +"\t"+ txtMuongH1.Text;//6
            Values = Values +"\t"+ txtMuongb1.Text;//7
            Values = Values +"\t"+ txtMuongb2.Text;//8
            Values = Values +"\t"+ txtMuongb3.Text;//9
            Values = Values +"\t"+ txtMuongH3.Text;//10
            Values = Values +"\t"+ txtLopdem.Text;//11
            regKey.SetValue("KtHothu", Values);
            regKey.Close();
            
        }
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtHothu").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                TxtSauDg.Text=STR[i++];//0
                TxtDayMongN.Text = STR[i++];//1
                txtDaymongT.Text = STR[i++];//2
                TxtDayThanhHt.Text = STR[i++];//3
                TxtMorongmong.Text = STR[i++];//4
                TxtRongHt.Text = STR[i++];//5
                txtMuongH1.Text = STR[i++];//6
                txtMuongb1.Text = STR[i++];//7
                txtMuongb2.Text = STR[i++];//8
                txtMuongb3.Text = STR[i++];//9
                txtMuongH3.Text = STR[i++];//10
                txtLopdem.Text = STR[i++];//11
                regKey.Close();
            }
        }

        // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.ht)|*.ht";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Ho thu
                Str = "------HO THU------" + "\r\n";
                Str = Str + "Cao ho thu H1:" +"\t"+ TxtCaoHt .Text + "\r\n";
                Str = Str + "Day mong trong H2:" +"\t"+ txtDaymongT .Text + "\r\n";
                Str = Str + "Day mong ngoai H3:" +"\t"+ TxtDayMongN .Text + "\r\n";
                Str = Str + "Day lop dem lot H4:" +"\t"+ txtLopdem .Text + "\r\n";
                Str = Str + "Sau dong ghet H5:" +"\t"+ TxtSauDg .Text + "\r\n";
                Str = Str + "Day dinh tuong b1:" +"\t"+ TxtDayThanhHt .Text + "\r\n";
                Str = Str + "Doc thanh ben i:" +"\t"+ txtDocthanh .Text + "\r\n";
                Str = Str + "Day chan tuong:" +"\t"+ txtDaychantuong .Text + "\r\n";
                Str = Str + "Gan theo do doc:" +"\t"+ optDoc .Text + "\r\n";
                Str = Str + "Gan theo be day:" +"\t"+ optDay .Text + "\r\n";
                Str = Str + "Mo rong mong b2:" +"\t"+ TxtMorongmong .Text + "\r\n";
                Str = Str + "Be rong doc B:" +"\t"+ TxtRongHt .Text + "\r\n";
                Str = Str + "Be rong ngang Bn:" +"\t"+ TxtNgangHt .Text + "\r\n";
                //Muong doc
                Str = Str + "------MUONG DOC------" + "\r\n";
                Str = Str + "Sau long muong H1:" +"\t"+ txtMuongH1 .Text + "\r\n";
                Str = Str + "Cao dinh muong H2:" +"\t"+ txtMuongH2 .Text + "\r\n";
                Str = Str + "Cao dinh tuong H3:" +"\t"+ txtMuongH3 .Text + "\r\n";
                Str = Str + "Cao dinh tuong H4:" +"\t"+ txtMuongH4 .Text + "\r\n";
                Str = Str + "Rong thanh vai b1:" +"\t"+ txtMuongb1 .Text + "\r\n";
                Str = Str + "Rong vai muong b2:" +"\t"+ txtMuongb2 .Text + "\r\n";
                Str = Str + "Rong long muong b3:" +"\t"+ txtMuongb3 .Text + "\r\n";

                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
        // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.ht)|*.ht";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Ho thu
                TxtCaoHt .Text=Values[i].Split('\t')[1]; i++;
                txtDaymongT .Text=Values[i].Split('\t')[1]; i++;
                TxtDayMongN .Text=Values[i].Split('\t')[1]; i++;
                txtLopdem .Text=Values[i].Split('\t')[1]; i++;
                TxtSauDg .Text=Values[i].Split('\t')[1]; i++;
                TxtDayThanhHt .Text=Values[i].Split('\t')[1]; i++;
                txtDocthanh .Text=Values[i].Split('\t')[1]; i++;
                txtDaychantuong .Text=Values[i].Split('\t')[1]; i++;
                optDoc .Text=Values[i].Split('\t')[1]; i++;
                optDay .Text=Values[i].Split('\t')[1]; i++;
                TxtMorongmong .Text=Values[i].Split('\t')[1]; i++;
                TxtRongHt .Text=Values[i].Split('\t')[1]; i++;
                TxtNgangHt .Text=Values[i].Split('\t')[1]; i++;
                //Muong doc
                i = i + 1;
                txtMuongH1 .Text=Values[i].Split('\t')[1]; i++;
                txtMuongH2 .Text=Values[i].Split('\t')[1]; i++;
                txtMuongH3 .Text=Values[i].Split('\t')[1]; i++;
                txtMuongH4 .Text=Values[i].Split('\t')[1]; i++;
                txtMuongb1 .Text=Values[i].Split('\t')[1]; i++;
                txtMuongb2 .Text=Values[i].Split('\t')[1]; i++;
                txtMuongb3.Text = Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }
        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmHoThu_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingFile();
        }
    }
}
